package com.smcdd.cdb.dbo.cdb_dbo.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.MainTenance;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Tain_v2;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.ui.adapter.TainAdapter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DateUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.TainDialog;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class DetailFragment extends Fragment {
    private final String id;
    private final String udid;
    private TextView mIdTv;

    private static final String ARG_PARAM1 = "param1";
    private List<Tain_v2> mParam1;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private TainDialog listDialog;
    private TainAdapter tainAdapter;
    private String date = "";
    private final String versionCode;
    private SwipeRefreshLayout page_srl;

    public DetailFragment() {
        versionCode = ObtainVersion.getVersionName(App.mContext);
        id = App.SP.getString("id", "");
        udid = App.SP.getString("udid", "");
        // Required empty public constructor
    }

    public static DetailFragment newInstance(List<Tain_v2> param1) {
        DetailFragment fragment = new DetailFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, (Serializable) param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = (List<Tain_v2>) getArguments().getSerializable(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_tain_pager, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.maintain_recycler);
        page_srl = (SwipeRefreshLayout) view.findViewById(R.id.maintain_srl);
        linearLayoutManager = new LinearLayoutManager(App.mContext);
        recyclerView.setLayoutManager(linearLayoutManager);

        setAdapter();
        return view;
    }

    private void setAdapter() {
        if (tainAdapter == null) {
            tainAdapter = new TainAdapter(App.mContext);
        }
        recyclerView.setAdapter(tainAdapter);
        tainAdapter.addData(mParam1);
        tainAdapter.setItemClickListenter((dataBean, position,flag,i) -> {
            long l = 0;
            if (this.date!= null&&!this.date.equals("")) {
                long time_long = DateUtils.getTime_long(date);
                l = time_long - 86400000;
            }else {
                l = System.currentTimeMillis() - 86400000;
            }
            String time = DateUtils.getTime(l);
            loadDiaolog(time,dataBean.getCharging_info_ids());
        });
    }

    /**
     * 获取dialog数据
     * @param date
     * @param charging_info_ids
     */
    public void loadDiaolog(String date,String charging_info_ids){
        String time = System.currentTimeMillis() + "";
        Map<String,String> map = new LinkedHashMap<>();
        map.put("rf","2");
        map.put("ts",time);
        map.put("v", versionCode);
        map.put("i", NetWorkCodeInfo.MAINTENANCE_CHARGING);
        map.put("udid", udid);
        map.put("eid", id);
        map.put("date",date);
        map.put("charging_info_ids",charging_info_ids);
        String encry1 = ObtainEncry.getEncry(map);
        map.put("encry",encry1);
        RetrofitFactory.getmInstanse().mApi.getmaintenance(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity<List<MainTenance>>>() {
                    @Override
                    public void success(BaseResponseEntity<List<MainTenance>> mainTenanceBaseResponseEntity) {
                        if (mainTenanceBaseResponseEntity.code == 200) {
                            List<MainTenance> data = mainTenanceBaseResponseEntity.data;
                            Dialog(data);
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.showToast(getActivity().getString(R.string.error_date));
                    }
                });
    }

    public void Dialog( List<MainTenance> data) {
        listDialog = new TainDialog(getContext(),data,"2");
        listDialog.show();
    }
}
